﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProductManager
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            if (!DBAccess.OpenConnection())
            {
                PathDatabase pathDatabase = new PathDatabase(this);
                pathDatabase.ShowDialog();
                this.Hide();
            }
        }

        public void SearchProduct(bool isRefresh)
        {
            DataTable products = new DataTable();
            if (!isRefresh)
                products = DBAccess.SearchProduct(txtSearch.Text, cboSearch.SelectedItem.ToString());
            else products = DBAccess.SearchProduct("", cboSearch.SelectedItem.ToString());
            if (products == null)
            {
                return;
            }
            lvProduct.Items.Clear();
            foreach (DataRow row in products.Rows)
            {
                lvProduct.Items.Add(new ListViewItem(new string[] {
                    row["Name"].ToString(), row["MinPrice"].ToString(), row["ID"].ToString() }));
            }
        }

        private void providersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProviderManager frmProviderManager = new ProviderManager();
            frmProviderManager.Show();
        }

        private void categoriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CategoryManager frmCategoryManager = new CategoryManager();
            frmCategoryManager.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            SearchProduct(true);
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DBAccess.CloseConnection();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddEditProduct frmNew = new AddEditProduct(this);
            frmNew.InitAdd();
            frmNew.ShowDialog();
        }

        private void btnManageProductCategory_Click(object sender, EventArgs e)
        {
            if (lvProduct.SelectedItems.Count == 1)
            {
                ProductCategories frmNew = new ProductCategories();
                frmNew.Text = "Product's Categories - " + lvProduct.SelectedItems[0].SubItems[0].Text;
                frmNew.ProductID = lvProduct.SelectedItems[0].SubItems[2].Text;
                frmNew.ShowDialog();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchProduct(false);
        }

        private void lvProduct_DoubleClick(object sender, EventArgs e)
        {
            if (lvProduct.SelectedItems.Count == 1)
            {
                AddEditProduct frmNew = new AddEditProduct(this);
                if (frmNew.InitEdit(lvProduct.SelectedItems[0].SubItems[2].Text))
                {
                    frmNew.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Please choose a product!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }
    }
}
